/********************************************
 title   : SuperPanel
 version : 1.5
 author  : Wietse Veenstra
 website : http://www.wietseveenstra.nl
 date    : 2007-03-30
 * modified by yisn to be used as a child of spark contains.
********************************************/
package com.desuntech.components {
	import com.desuntech.skins.SuperPanelSkin;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.core.IVisualElementContainer;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.managers.CursorManager;
	import mx.managers.IFocusManagerComponent;
	
	import spark.components.Group;
	import spark.components.TileGroup;
	
	public class SuperPanel extends Panel implements IFocusManagerComponent {
		public static const MIN_PANEL:String = 'minPanel';
		
		[Bindable] public var enableResize:Boolean = false;
		[Bindable] public var enableMin:Boolean = false;
		[Bindable] public var enableNormalMax:Boolean = false;
		[Bindable] public var enableDrag:Boolean = false;
		[Bindable] public var enableClose:Boolean = false;
				
		[Embed(source="/assets/img/resizeCursor.png")]
		private static var resizeCursor:Class;
		
		public var type:String = 'module_view';
		
		private var pTitleBar:UIComponent;
		public var oW:Number;
		public var oH:Number;
		public var oX:Number;
		public var oY:Number;
		public var xBeforeMin:Number;
		public var yBeforeMin:Number;
		public var wBeforeMin:Number;
		public var hBeforeMin:Number;
		
		private var minButton:Button		= new Button();
		private var normalMaxButton:Button	= new Button();
		private var closeButton:Button		= new Button();
		private var resizeHandler:Button	= new Button();
		
		private var oPoint:Point 			= new Point();
		private var resizeCur:Number		= 0;
		public var node:XML;
				
		public function SuperPanel(n:XML = null) {
			if(n) {
				this.node = n;
			}
			this.minHeight = 100;
			this.minWidth = 150;
			this.layout = 'absolute';
			this.horizontalScrollPolicy = 'off';
			this.verticalScrollPolicy = 'off;'
			this.addEventListener(FocusEvent.FOCUS_IN, onFocusInChange);
		}
		
		private function onFocusInChange(e:FocusEvent):void {
			if(parent) {
				if(parent is TileGroup) {
					
				} else {
					if(IVisualElementContainer(parent).getElementIndex(this) < IVisualElementContainer(parent).numElements-1){
						IVisualElementContainer(parent).setElementIndex(this, IVisualElementContainer(parent).numElements-1);
					}
				}
			}
		}
		
		public function removeControls():void {
			removeListeners();
			if (enableResize) 		this.rawChildren.removeChild(resizeHandler);
			if (enableMin) 			this.pTitleBar.removeChild(minButton);
			if (enableNormalMax) 	this.pTitleBar.removeChild(normalMaxButton);
			if (enableClose) 		this.pTitleBar.removeChild(closeButton);
		}
		
		public function addControls():void {
			addListeners();
			if (enableResize) 		this.rawChildren.addChild(resizeHandler);
			if (enableMin) 			this.pTitleBar.addChild(minButton);
			if (enableNormalMax) 	this.pTitleBar.addChild(normalMaxButton);
			if (enableClose) 		this.pTitleBar.addChild(closeButton);
		}

		override protected function createChildren():void {
			super.createChildren();
			this.pTitleBar = super.titleBar;
			if(node) this.title = node.@label;
			this.setStyle('titleBackgroundSkin', com.desuntech.skins.SuperPanelSkin);
			this.setStyle('titleStyleName', 'mypanelTitle');
			// halo theme
			/*this.setStyle("headerColors", [0xC3D1D9, 0x5A788A]);
			this.setStyle("borderColor", 0x5A788A);*/
			
			if (enableResize) {
				this.resizeHandler.width     = 12;
				this.resizeHandler.height    = 12;
				this.resizeHandler.styleName = "resizeHndlr";
				this.rawChildren.addChild(resizeHandler);
				this.initPos();
			}
			
			if (enableMin) {
				this.minButton.width     	= 10;
				this.minButton.height    	= 10;
				this.minButton.styleName 	= "minimizeBtn";
				this.pTitleBar.addChild(this.minButton);
			}
			
			if (enableNormalMax) {
				this.normalMaxButton.width     	= 10;
				this.normalMaxButton.height    	= 10;
				this.normalMaxButton.styleName 	= "increaseBtn";
				this.pTitleBar.addChild(this.normalMaxButton);
			}
			
			
			if (enableClose) {
				this.closeButton.width     		= 10;
				this.closeButton.height    		= 10;
				this.closeButton.styleName 		= "closeBtn";
				this.pTitleBar.addChild(this.closeButton);
			}
			
			this.positionChildren();	
			this.addListeners();
		}
		
		public function initPos():void {
			this.oW = this.width;
			this.oH = this.height;
			this.oX = this.x;
			this.oY = this.y;
		}
	
		public function positionChildren():void {
			var i:int = 0;
			
			if (enableClose) {
				this.closeButton.buttonMode    = true;
				this.closeButton.useHandCursor = true;
				this.closeButton.x = this.unscaledWidth - this.minButton.width - 8-16*i;
				this.closeButton.y = 8;
				i++;
			}
			
			if (enableNormalMax) {
				this.normalMaxButton.buttonMode    = true;
				this.normalMaxButton.useHandCursor = true;
				this.normalMaxButton.x = this.unscaledWidth - this.minButton.width - 8 - 16*i;
				this.normalMaxButton.y = 8;
				i++;
			}
			
			if (enableMin) {
				this.minButton.buttonMode    = true;
				this.minButton.useHandCursor = true;
				this.minButton.x = this.unscaledWidth - this.minButton.width - 8 - 16*i;
				this.minButton.y = 8;
				i++;
			}
			
			if (enableResize) {
				this.resizeHandler.y = this.unscaledHeight - resizeHandler.height - 1;
				this.resizeHandler.x = this.unscaledWidth - resizeHandler.width - 1;
			}
		}
		
		public function addListeners():void {
			if (enableDrag) {
				this.pTitleBar.addEventListener(MouseEvent.MOUSE_DOWN, titleBarDownHandler);
				this.pTitleBar.addEventListener(MouseEvent.MOUSE_UP, titleBarUpHandler);
			}
			
			if (enableMin) {
				this.minButton.addEventListener(MouseEvent.CLICK, minClickHandler);
			}
			
			if (enableNormalMax) {
				this.normalMaxButton.addEventListener(MouseEvent.CLICK, normalMaxClickHandler);
			}
			
			if (enableClose) {
				this.closeButton.addEventListener(MouseEvent.CLICK, closeClickHandler);
			}
			
			if (enableResize) {
				this.resizeHandler.addEventListener(MouseEvent.MOUSE_OVER, resizeOverHandler);
				this.resizeHandler.addEventListener(MouseEvent.MOUSE_OUT, resizeOutHandler);
				this.resizeHandler.addEventListener(MouseEvent.MOUSE_DOWN, resizeDownHandler);
			}
		}
		
		public function removeListeners():void {
			if (enableDrag) {
				this.pTitleBar.removeEventListener(MouseEvent.MOUSE_DOWN, titleBarDownHandler);
				this.pTitleBar.removeEventListener(MouseEvent.MOUSE_UP, titleBarUpHandler);
			}
			
			if (enableMin) {
				this.minButton.removeEventListener(MouseEvent.CLICK, minClickHandler);
			}
			
			if (enableNormalMax) {
				this.normalMaxButton.removeEventListener(MouseEvent.CLICK, normalMaxClickHandler);
			}
			
			if (enableClose) {
				this.closeButton.removeEventListener(MouseEvent.CLICK, closeClickHandler);
			}
			
			if (enableResize) {
				this.resizeHandler.removeEventListener(MouseEvent.MOUSE_OVER, resizeOverHandler);
				this.resizeHandler.removeEventListener(MouseEvent.MOUSE_OUT, resizeOutHandler);
				this.resizeHandler.removeEventListener(MouseEvent.MOUSE_DOWN, resizeDownHandler);
			}
		}
		
		public function titleBarDownHandler(event:MouseEvent):void {
			this.pTitleBar.addEventListener(MouseEvent.MOUSE_MOVE, titleBarMoveHandler);
		}
		
		public function titleBarUpHandler(event:MouseEvent):void {
			this.pTitleBar.removeEventListener(MouseEvent.MOUSE_MOVE, titleBarMoveHandler);
		}
			
		public function titleBarMoveHandler(event:MouseEvent):void {
			this.pTitleBar.removeEventListener(MouseEvent.MOUSE_MOVE, titleBarMoveHandler);
			if (this.width < screen.width) {
				(this.parent as Group).addEventListener(MouseEvent.MOUSE_UP, titleBarDragDropHandler);
				this.pTitleBar.addEventListener(DragEvent.DRAG_DROP,titleBarDragDropHandler);
				
				(this.parent as Group).setElementIndex(this, (this.parent as Group).numElements - 1);
				this.alpha = 0.5;
				this.startDrag(false, new Rectangle(0, 0, screen.width - this.width, screen.height - this.height));
			}
		}
		
		public function titleBarDragDropHandler(event:Event):void {
			(this.parent as Group).removeEventListener(MouseEvent.MOUSE_UP, titleBarDragDropHandler);
			this.pTitleBar.removeEventListener(DragEvent.DRAG_DROP,titleBarDragDropHandler);
			
			this.alpha = 1.0;
//			this.oX = this.x;
//			this.oY = this.y;
			this.stopDrag();
		}

		public function normalMaxClickHandler(event:MouseEvent):void {
			if (this.normalMaxButton.styleName == "increaseBtn") {
				this.initPos();
				this.x = 0;
				this.y = 0;
				this.width = screen.width;
				this.height = screen.height;
				this.normalMaxButton.styleName = "decreaseBtn";
				this.positionChildren();
			} else {
				this.x = this.oX;
				this.y = this.oY;
				this.width = this.oW;
				this.height = this.oH;
				this.normalMaxButton.styleName = "increaseBtn";
				this.positionChildren();
			}
		}
		
		public function minClickHandler(event:MouseEvent):void {
			dispatchEvent(new Event(MIN_PANEL));
		}
		
		public function closeClickHandler(event:MouseEvent):void {
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
		}
		
		public function resizeOverHandler(event:MouseEvent):void {
			this.resizeCur = CursorManager.setCursor(resizeCursor);
		}
		
		public function resizeOutHandler(event:MouseEvent):void {
			CursorManager.removeCursor(CursorManager.currentCursorID);
		}
		
		public function resizeDownHandler(event:MouseEvent):void {
			(this.parent as Group).addEventListener(MouseEvent.MOUSE_MOVE, resizeMoveHandler);
			(this.parent as Group).addEventListener(MouseEvent.MOUSE_UP, resizeUpHandler);
			stage.addEventListener(Event.MOUSE_LEAVE, resizeUpHandler);
			
			this.resizeHandler.addEventListener(MouseEvent.MOUSE_OVER, resizeOverHandler);
			this.resizeCur = CursorManager.setCursor(resizeCursor);
			this.oPoint.x = mouseX;
			this.oPoint.y = mouseY;
			this.oPoint = this.localToGlobal(oPoint);		
		}
		
		public function resizeMoveHandler(event:MouseEvent):void {
			this.stopDragging();

			var xPlus:Number = (this.parent as Group).mouseX - this.oPoint.x;			
			var yPlus:Number = (this.parent as Group).mouseY - this.oPoint.y;
			
			if (this.oW + xPlus > minWidth) {
				this.width = this.oW + xPlus;
			}
			
			if (this.oH + yPlus > minHeight) {
				this.height = this.oH + yPlus;
			}
			this.positionChildren();
		}
		
		public function resizeUpHandler(event:Event):void {
			(this.parent as Group).removeEventListener(MouseEvent.MOUSE_MOVE, resizeMoveHandler);
			(this.parent as Group).removeEventListener(MouseEvent.MOUSE_UP, resizeUpHandler);
			stage.removeEventListener(Event.MOUSE_LEAVE, resizeUpHandler);
			
			CursorManager.removeCursor(CursorManager.currentCursorID);
			this.resizeHandler.addEventListener(MouseEvent.MOUSE_OVER, resizeOverHandler);
			this.initPos();
		}
	}
	
}